package com.ifkytek.ninegridview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ScoreProgressLevel extends View {

    private Context mContext;
    private int color_active;
    private int color_inactive;


    private Paint activePaint;
    private Paint inactiviPaint;


    private float active_size;
    private float inactivie_size;
    private Bitmap cursorBitmap;
    private int bitmapHeight;
    private int bitmapWidth;
    private Paint bitmapPaint;
    private float pro_length;
    private float modify_width = 0f;
    private float defalutMargin = 60.f;
    private RectF bitMapRect;
    private float bitMapRectleft;
    private float bitMapRecttop;
    private Paint pro_text;
    private int cursor_text_color;
    private float cursor_text_size;
    private String cursor_text;
    private float text_left;
    private float text_top;
    private float modify_height;


    private float progress = 0;
    private float cursor_margin_progress;
    private float margin_progress;


    public ScoreProgressLevel(Context context) {
        this(context, null);
    }

    public ScoreProgressLevel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScoreProgressLevel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initType(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        //进度条画笔
        activePaint = new Paint();
        activePaint.setColor(color_active);
        activePaint.setStrokeWidth(active_size);
        activePaint.setStrokeCap(Paint.Cap.ROUND);
        activePaint.setAntiAlias(true);
        inactiviPaint = new Paint();
        inactiviPaint.setColor(color_inactive);
        inactiviPaint.setStrokeWidth(inactivie_size);
        inactiviPaint.setStrokeCap(Paint.Cap.ROUND);
        inactiviPaint.setAntiAlias(true);
        //图片画笔
        bitmapPaint = new Paint();
        modify_width  = bitmapWidth*0.56f;
        modify_height = bitmapHeight*0.7f;
        Log.d("wangbang","modify_height="+modify_height);
        //图片文字画笔
        pro_text = new Paint();
        pro_text.setColor(cursor_text_color);
        pro_text.setTextSize(cursor_text_size);
        bitMapRectleft = defalutMargin;
        bitMapRecttop = defalutMargin;
        getCursorTextPosition("0");


        margin_progress = defalutMargin + cursor_margin_progress;
    }


    private void initType(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ScoreProgressLevel, defStyleAttr, 0);

        pro_length = ta.getDimension(R.styleable.ScoreProgressLevel_sp_length, 600f);

        color_active = ta.getColor(R.styleable.ScoreProgressLevel_sp_active_color, context.getResources().getColor(R.color.colorAccent));
        color_inactive = ta.getColor(R.styleable.ScoreProgressLevel_sp_inactivie_corlor, context.getResources().getColor(R.color.white));

        cursor_text_color = ta.getColor(R.styleable.ScoreProgressLevel_sp_cursor_text_color, context.getResources().getColor(R.color.white));
        cursor_text_size = ta.getDimension(R.styleable.ScoreProgressLevel_sp_cursor_text_size, 30f);


        cursor_margin_progress = ta.getDimension(R.styleable.ScoreProgressLevel_sp_cursor_margin_progress, 0f);


        cursor_text = ta.getString(R.styleable.ScoreProgressLevel_sp_cursor_text);
        if (cursor_text ==null){
            cursor_text = "0";
        }
        active_size = ta.getDimension(R.styleable.ScoreProgressLevel_sp_active_size, 10f);
        inactivie_size = ta.getDimension(R.styleable.ScoreProgressLevel_sp_inactive_size, 8f);

        int resourceId = ta.getResourceId(R.styleable.ScoreProgressLevel_sp_ic_progress_cursor, R.mipmap.ic_progress_cursor);
        cursorBitmap = BitmapFactory.decodeResource(getResources(), resourceId);
        bitmapHeight = cursorBitmap.getHeight();
        bitmapWidth = cursorBitmap.getWidth();
        ta.recycle();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        //获得控件宽高
//        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
//        int heightmode = MeasureSpec.getMode(heightMeasureSpec);
//
//        int width = MeasureSpec.getSize(widthMeasureSpec);
//        int height = MeasureSpec.getSize(heightMeasureSpec);
//
//        Log.d("wangbang","布局宽="+width+"布局高"+height);
//

//        Log.d("wangbang","布局真实宽="+realwidth+"布局真实高"+realHeight);
//
//        int makeMeasurewidth = MeasureSpec.makeMeasureSpec(realwidth, MeasureSpec.UNSPECIFIED );
//        int measureSpecHeight = MeasureSpec.makeMeasureSpec(heightmode, MeasureSpec.UNSPECIFIED );
//        setMeasuredDimension(makeMeasurewidth,measureSpecHeight);

        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        int paddingTop = getPaddingTop();
        int realwidth = (int)(modify_width + pro_length +defalutMargin*2);
        int realHeight = (int)(bitmapHeight+active_size+defalutMargin*2);
        realHeight = resolveSize(realHeight + paddingBottom + paddingTop, heightMeasureSpec);
        realwidth = resolveSize(realwidth + paddingLeft + paddingRight, widthMeasureSpec);
        setMeasuredDimension(realwidth, realHeight);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(cursorBitmap,bitMapRectleft+pro_length*progress,bitMapRecttop,bitmapPaint);
        canvas.drawText(cursor_text,text_left+pro_length*progress,text_top,pro_text);
        //底色条纹
        canvas.drawLine(modify_width+defalutMargin,bitmapHeight+active_size+margin_progress,
                modify_width+defalutMargin+pro_length,bitmapHeight+active_size+margin_progress,inactiviPaint);
        //进度条纹
        canvas.drawLine(modify_width+defalutMargin,bitmapHeight+active_size+margin_progress,
                modify_width+defalutMargin+pro_length*progress,
                bitmapHeight+active_size+margin_progress,activePaint);
    }

    public void setTotalScoreAndCurrent(int totalScore,int currentScore){
        if (totalScore<currentScore) return;
        cursor_text = currentScore+"";  //显示分数
        getCursorTextPosition(cursor_text);
        progress = currentScore*1.0f/totalScore*1.0f; //当前进度
        Log.d("wangbang","精度条1000长度="+pro_length*progress);
        postInvalidate();
    }

    public void updateProgress(int totalScore,int currentScore){

        cursor_text = currentScore+"";  //显示分数
        getCursorTextPosition(cursor_text);
        if (totalScore<currentScore) return;
        final float caculateprogress = progress;
        float currentProgress =  currentScore*1.0f/totalScore*1.0f;
        float abs = currentProgress - caculateprogress;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, abs);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Float animatedValue = (Float) valueAnimator.getAnimatedValue();
                progress = caculateprogress+animatedValue;
                postInvalidate();
            }
        });
        valueAnimator.start();
    }

    private void getCursorTextPosition(String cursor_text) {
        float text_width = pro_text.measureText(cursor_text);
        float text_left_alin = (bitmapWidth - text_width) / 2;
        float text_center_margin = (modify_height - cursor_text_size) / 2;
        text_left = bitMapRectleft+text_left_alin;
        text_top = bitMapRecttop+text_center_margin*0.8f+cursor_text_size;
    }

}
